package com.small.act.map;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;

import com.small.R;
import com.small.act.discount.DiscountDetailActivity;
import com.small.act.discount.FavDiscountActivity;
import com.small.adapter.DiscountAdapter;
import com.small.commom.Action;
import com.small.commom.AsyncDataLoader;
import com.small.commom.AsyncDataLoader.Callback;
import com.small.commom.Common;
import com.small.interfaces.Task;
import com.small.interfaces.Util;
import com.small.model.DiscountModel;
import com.small.model.SQChildModel;
import com.small.model.SQParentModel;
import com.small.model.ShopChildModel;
import com.small.model.ShopParentModel;
import com.small.view.custview.dialog.MyPopMenu;

public class CopyOfDiscountNearbyActivity extends Activity implements Task{

	ListView list_view;
	EditText txt_find;
	ImageButton btn_search, btn_back, btn_discount, btn_gps, btn_fav,btn_map;
//	LinearLayout layout_gps;
	Button btn_region, btn_type, btn_order;

	ArrayList<String> list_Shopfirst_name = null;
	ArrayList<String> list_Shopsecond_name = null;
//	ArrayList<String> list_SQfirst_name = null;
	ArrayList<String> list_second_distance = null;
	ArrayList<String> list_second_ordername = null;
	MyPopMenu pop;
	Context ctx;
	Action action;
	List<ShopParentModel> shoplist;
	List<SQParentModel> sqlist;
	List<ShopChildModel> childShopList;
	List<SQChildModel> childSQList;
	private final int SHOP_TYPE_FIRST = 0;
	private final int SHOP_TYPE_SECOND = 1;
	private final int SQ_TYPE_FIRST = 2;
	private final int SQ_TYPE_SECOND = 3;
	private final int ORDER_TAG = 4;

	private HashMap<String, Object> favDiscountMap = null;
	ArrayList<DiscountModel> discountList = null;
	DiscountAdapter list_adapter;
	private int result;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_nearby_view);
		findView();
		initData();
	}

	private void initData() {
		Util.removeActivity();
		Util.addActivity(this);
		ctx = CopyOfDiscountNearbyActivity.this;
		action = new Action(ctx);
		Resources res = ctx.getResources();
		list_second_ordername = new ArrayList<String>();
		list_second_ordername.add(res.getString(R.string.nearest));
		list_second_ordername.add(res.getString(R.string.latest));
		list_second_ordername.add(res.getString(R.string.welcome));
		list_second_distance =  new ArrayList<String>();
		list_second_distance.add("500米");
		list_second_distance.add("1000米");
		list_second_distance.add("2000米");
		list_second_distance.add("3000米"); 
		list_second_distance.add("5000米");
		//	// final CharSequence[] items = { "500M", "1000M", "2000M", "3000M" };

		action.showProgressDialog("正在加载...");
		new AsyncDataLoader(asyncCallback).execute();
	}

	public void findView() { 
		btn_region = (Button) this.findViewById(R.id.btn_filter_region);
		btn_type = (Button) this.findViewById(R.id.btn_filter_category);
		btn_order = (Button) this.findViewById(R.id.btn_filter_order);
		btn_order.setOnClickListener(onClick);
		btn_type.setOnClickListener(onClick);
		btn_region.setOnClickListener(onClick);
	 
		btn_back = (ImageButton) this.findViewById(R.id.btn_back); 
		btn_back.setOnClickListener(onClick);
		btn_map = (ImageButton) this.findViewById(R.id.btn_map); 
		btn_map.setOnClickListener(onClick);

		list_view = (ListView) this.findViewById(R.id.list_view);
		list_view.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view,
					int position, long arg3) {
				startActivity(new Intent(CopyOfDiscountNearbyActivity.this, DiscountDetailActivity.class));
			}
		});  
//		btn_discount = (ImageButton) this.findViewById(R.id.btn_discount);
//		btn_gps = (ImageButton) this.findViewById(R.id.btn_gps);
//		btn_fav = (ImageButton) this.findViewById(R.id.btn_fav);
//		btn_discount.setOnClickListener(onClick);
//		btn_gps.setOnClickListener(onClick);
//		btn_fav.setOnClickListener(onClick);
	}

	private Callback asyncCallback = new AsyncDataLoader.Callback() {

		@Override
		public void onStart() { 
			// action.getFavDiscountInfo(Common.model.getMemberID(), 2, 1);
			favDiscountMap = action.getDiscountInfobydistance(1, 1, "1", 5, 1,117.15863, 36.65231, 10000, "运动", "休闲"); 
		}

		@Override
		public void onPrepare() {
		}

		@SuppressWarnings("unchecked")
		@Override
		public void onFinish() { 
			action.stopProgressDialog(); 
			if (favDiscountMap != null) {
				Integer i = (Integer) favDiscountMap.get("result");
				result = (int) i; 
				discountList = (ArrayList<DiscountModel>) favDiscountMap .get("discountList"); 
				resultMethod(result, discountList);
			} else {
				resultMethod(-2, null);
			} 
		}
	};

	protected void resultMethod(int result2, List<DiscountModel> mydiscountlist) {
		switch (result2) {
		case 0:
			action.showShortToast("请求失败，请稍候再试...");
			break;
		case 1:
			if (mydiscountlist != null) {
				list_adapter = new DiscountAdapter(CopyOfDiscountNearbyActivity.this, mydiscountlist);
				list_view.setAdapter(list_adapter);
			} else {
				action.showShortToast("请求失败，请稍候再试...");
			}
			break;
		case -2:
			action.showShortToast("数据异常，请求失败...");
			break;
		default:
			break;
		}
	}

	OnClickListener onClick = new OnClickListener() {

		@Override
		public void onClick(View v) { 
			switch (v.getId()) {
			case R.id.btn_home:
				finish();
				break;
			case R.id.btn_filter_region:
				regionDialog(btn_region);
				break;
			case R.id.btn_filter_category:
				filterCategory(btn_type);
				break;
			case R.id.btn_filter_order:
				filterOrder(btn_order); 
				break;
			 
			case R.id.btn_map:
				 
//				Thinksns.getInstance(NearDiscountActivity.this).goToNearByMap(discountList);
				Intent ii = new Intent(CopyOfDiscountNearbyActivity.this, MapLocationOverlay.class); 
				Bundle mBundle = new Bundle();
				mBundle.putSerializable("discountList", discountList);
				ii.putExtras(mBundle); 
				CopyOfDiscountNearbyActivity.this.startActivity(ii); 
				break; 
		  
			
			case R.id.bottom_myyhq:
				Intent i = new Intent(CopyOfDiscountNearbyActivity.this, FavDiscountActivity.class);
				CopyOfDiscountNearbyActivity.this.startActivity(i);
				break;
			}
		}
	};

	//分类筛选
	private void filterCategory(View view) { 
		shoplist = action.getParentShopInfo(); 
		if (shoplist != null) {
			list_Shopfirst_name = new ArrayList<String>();
			for (ShopParentModel sqM : shoplist) {
				list_Shopfirst_name.add(sqM.getCategoryName());
			}
			list_Shopsecond_name = childShoplist(shoplist.get(0) .getCategoryID());
		}
		pop = new MyPopMenu(CopyOfDiscountNearbyActivity.this, list_Shopfirst_name, list_Shopsecond_name);
		pop.initPopupWindow(R.drawable.filter_left_bg, R.layout.choice_single_listpop_layout, SHOP_TYPE_FIRST, SHOP_TYPE_SECOND);
		pop.show(view);
		pop.refresh(0,0, list_Shopsecond_name);
	}

	//排序筛选
	protected void filterOrder(View view) {
		pop = new MyPopMenu(CopyOfDiscountNearbyActivity.this, null,list_second_ordername);
		pop.initPopupWindow(R.drawable.filter_right_bg,R.layout.choice_single_listpop_layout, pop.VIEW_NONE_TAG, ORDER_TAG);
		pop.show(view); 
	}

	//
	private ArrayList<String> childShoplist(int id) {
		ArrayList<String> lists = null;
		childShopList = action.getChildShopInfo(id);
		if (childShopList != null) {
			lists = new ArrayList<String>();
			for (ShopChildModel sqCM : childShopList) {
				lists.add(sqCM.getCategoryName());
			}
		}
		return lists;
	}

	//按商圈距离筛选
	private void regionDialog(View view) {
		pop = new MyPopMenu(CopyOfDiscountNearbyActivity.this, null,list_second_distance);
		pop.initPopupWindow(R.drawable.filter_mid_bg,R.layout.choice_single_listpop_layout, pop.VIEW_NONE_TAG, SQ_TYPE_SECOND);
		pop.show(view); 
	}

	private ArrayList<String> childSQlist(int id) {
		ArrayList<String> lists = null;
		childSQList = action.getChildSQInfo(id);
		if (childSQList != null) {
			lists = new ArrayList<String>();
			for (SQChildModel sqCM : childSQList) {
				lists.add(sqCM.getCategoryName());
			}
		}
		return lists;
	}

	// public void ShowFindType() {
	// final CharSequence[] items = { "离我最近", "人气最高", "最新发布" };
	// AlertDialog dlg = new AlertDialog.Builder(DiscountHomeActivity.this)
	// .setTitle("排序方式")
	// .setItems(items, new DialogInterface.OnClickListener() {
	// public void onClick(DialogInterface dialog, int item) {
	// intOrderType = item + 1;
	// btn_order.setText(items[item]);
	// // onData();
	// }
	// }).create();
	// dlg.show();
	// }

	// public void ShowFindJuli() {
	// final CharSequence[] items = { "500M", "1000M", "2000M", "3000M" };
	// AlertDialog dlg = new AlertDialog.Builder(DiscountHomeActivity.this)
	// .setTitle("请选择")
	// .setItems(items, new DialogInterface.OnClickListener() {
	// public void onClick(DialogInterface dialog, int item) {
	// switch (item) {
	// case 0:
	// intJuli = 500;
	// break;
	// case 1:
	// intJuli = 1000;
	// break;
	// case 2:
	// intJuli = 2000;
	// break;
	// case 3:
	// intJuli = 3000;
	// break;
	// }
	// btn_region.setText(items[item]);
	// // onData();
	// }
	// }).create();
	// dlg.show();
	// }

	@Override
	public void onRefresh(int page, HashMap<String, Object> map) {
		switch (page) {
		case SHOP_TYPE_FIRST:
			Integer i = (Integer) map.get("position");		 
			list_Shopsecond_name = childShoplist(shoplist.get((int) i).getCategoryID());
			pop.refresh(0,0, list_Shopsecond_name);
			break;
		case SHOP_TYPE_SECOND:
			Integer i2 = (Integer) map.get("position"); 
			Toast.makeText(ctx, "您选择了第" + i2 + list_Shopsecond_name.get(i2),Toast.LENGTH_SHORT).show();
			pop.dismiss();
			break;
		case SQ_TYPE_FIRST:
			Integer i3 = (Integer) map.get("position"); 
			list_second_distance = childSQlist(sqlist.get((int) i3).getCategoryID());
			pop.refresh(0,0, list_second_distance);
			break;
		case SQ_TYPE_SECOND:
			Integer i4 = (Integer) map.get("position"); 
			Toast.makeText(ctx, "您选择了第" + i4 + list_second_distance.get(i4), Toast.LENGTH_SHORT).show();
			pop.dismiss();
			break;
		case ORDER_TAG:
			Integer i5 = (Integer) map.get("position"); 
			Toast.makeText(ctx, "您选择了第" + i5 + list_second_ordername.get(i5), Toast.LENGTH_SHORT).show();
			pop.dismiss();
			break;
		}
	}

	@Override
	protected void onDestroy() {
		if (Common.dbtool != null) {
			Common.dbtool.close();
			Common.dbtool = null;
		}
		super.onDestroy();
	}
}